﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ASoft.Repository;
using System.Collections;
using ASoft.Base;

namespace ASoft.WPF.ViewModel
{
    public partial class RoomInfoViewModel : DBSearchViewModel<RoomInfoEntity>
    {
        protected override IEnumerable<RoomInfoEntity> OrderSearchResult(IEnumerable<RoomInfoEntity> items)
        {
            return items.OrderBy(p => p.Id).OrderByDescending(p => p.时间);
        }

        protected override IRepository<RoomInfoEntity> Repository
        {
            get { return new RoomInfoRepository(); }
        }

        protected override IEnumerable<SeriesBindObject> GetSeriesBindObjects(string propertyName)
        {
            var result = this.数据集合.GroupBy(p => p.Id)
                 .Select(p => GetSeriesBindObject(p.Key, propertyName, p.AsEnumerable()))
                 .ToList();
            return result;
        }

        private SeriesBindObject GetSeriesBindObject(int id, string propertyName, IEnumerable<RoomInfoEntity> iEnumerator)
        {
            var property = typeof(RoomInfoEntity).GetProperty(propertyName);
            var dictType = typeof(Dictionary<,>).MakeGenericType(typeof(DateTime), property.PropertyType);
            var dict = Activator.CreateInstance(dictType) as IEnumerable;
            var addMethod = dictType.GetMethod("Add");
            foreach (var item in iEnumerator)
            {
                addMethod.Invoke(dict, new object[] { item.时间, property.GetValue(item, null) });
            }

            return new SeriesBindObject("Id:" + id.ToString() + "," + propertyName, dict);
        }
    }
}
